﻿package game.account.preview {
	import game.alternativa.ModelViewer.ModelViewer;
	import flash.events.Event;
	import game.alternativa.Model;
	import game.lang.Lang;
	import game.alternativa.ModelEvent;
	import game.objects.TextureObject;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.resources.TextureResource;
	
	public class Preview extends ModelViewer {
		
		private var BODY_CONSIST:Array	= [PreviewObject.BODY, PreviewObject.LIGHTS, PreviewObject.LIGHTS_WINDOW, PreviewObject.WINDOW, PreviewObject.SPOILER];
		private var WHEEL_CONSIST:Array	= [PreviewObject.WHEEL1, PreviewObject.WHEEL2, PreviewObject.WHEEL3, PreviewObject.WHEEL4];
		private var OTHER_CONSIST:Array	= [PreviewObject.THRESHOLD, PreviewObject.HOOD, PreviewObject.BUMPER_FONT, PreviewObject.BUMPER_BACK];
		private var STATUS_ARRAY:Array	= [];
		
		public function Preview():void {
			this.addEventListener(Event.ADDED_TO_STAGE, added);
		}
		
		private function added(event:Event):void {
			
			loadBody	(1, 1);
			for each (var type:String in OTHER_CONSIST)
				loadDetail	(type, 1, 1, 1);
			loadWheel	(1, 1, 2);
			
			//(objects[PreviewObject.WINDOW] as Model).addEventListener(ModelEvent.TEXTURE_COMPLETE, windowsComplete);
		}
		
		public function loadBody(body_id:int, texture_id:int):void {
			for each (var type:String in BODY_CONSIST) {
				this.a3d ( type, getBodyModel(type, body_id) );
			}
			
			this.textures	( PreviewObject.BODY,				new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE,		getTexture(PreviewObject.BODY, texture_id) ),
				new TextureObject ( TextureObject.SPECULAR,		0xffffff )
				//new TextureObject ( TextureObject.ENVIRONMENT,param.cubeGarage ),
				//new TextureObject ( TextureObject.REFLECTION,	param.reflect )
				//new TextureObject ( TextureObject.BUMP,		config.host+config.dirs.textures+PreviewObject.BODY+'/bump'+String(texture_id)+'.jpg' ),
				//new TextureObject ( TextureObject.LIGHT,		config.host+config.dirs.textures+PreviewObject.BODY+'/bump'+String(texture_id)+'.jpg' )
			] );
			
			var lights_window:Model = getModel(PreviewObject.LIGHTS_WINDOW);
			lights_window.alpha = 0.5;
			this.textures	( PreviewObject.LIGHTS_WINDOW,		new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE,		0x000000 ),
				new TextureObject ( TextureObject.SPECULAR,		0xffffff )
			] );
			
			this.textures	( PreviewObject.LIGHTS,				new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE,		getTexture(PreviewObject.LIGHTS, texture_id) )
			] );
			
			var window:Model = getModel(PreviewObject.WINDOW);
			window.alpha = 0.7;
			this.textures	( PreviewObject.WINDOW,				new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE,		0x000000 ),
				new TextureObject ( TextureObject.SPECULAR,		0xffffff )
			] );
			
			
			this.textures	( PreviewObject.SPOILER,			new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE,		getTexture(PreviewObject.SPOILER, texture_id) ),
				new TextureObject ( TextureObject.SPECULAR,		0xffffff )
			] );
			
			config.autoUtil.setSpoiler(body_id, getModel(PreviewObject.SPOILER));
			
		}
		
		public function loadWheel(body_id:int, detail_id:int, texture_id:int):void {
			for each (var type:String in WHEEL_CONSIST) {
				this.a3d ( type, getDetailModel(PreviewObject.WHEEL, body_id, detail_id) );
				this.textures ( type, new <TextureObject>[
					new TextureObject ( TextureObject.DIFFUSE, getTexture(PreviewObject.WHEEL, texture_id) ),
					new TextureObject ( TextureObject.SPECULAR, 0xffffff )
				] );
			}
			
			if (WHEEL_CONSIST.length==4)
				config.autoUtil.setWheels(body_id, getModel(WHEEL_CONSIST[0]), getModel(WHEEL_CONSIST[1]), getModel(WHEEL_CONSIST[2]), getModel(WHEEL_CONSIST[3]));
			
		}
		
		public function loadDetail(type:String, body_id:int, detail_id:int, texture_id:int):void {
			STATUS_ARRAY[type] = false;
			this.a3d ( type, getDetailModel(type, body_id, detail_id) );
			this.textures ( type, new <TextureObject>[
				new TextureObject ( TextureObject.DIFFUSE, getTexture(type, texture_id) ),
				new TextureObject ( TextureObject.SPECULAR,		0xffffff )
			] );
		}
		
		private function getTexture(type:String, texture_id:int):String {
			return Lang.regexp(config.dirs.URL_texture, {host: config.host, dir: config.dirs.textures, type: type, id: String(texture_id)})
		}
		
		private function getBodyModel(type:String, body_id:int):String {
			return Lang.regexp(config.dirs.URL_body_models, {host: config.host, dir: config.dirs.models, type: type, id: String(body_id)})
		}
		
		private function getDetailModel(type:String, body_id:int, detail_id:int):String {
			return Lang.regexp(config.dirs.URL_detail_models, {host: config.host, dir: config.dirs.models, type: type, id: String(body_id), detail: String(detail_id)})
		}
		
	}
	
}